connect
connect establishes a connection with another host.
Syntax
int connect( SOCKET s, const struct sockaddr *name, int namelen );
Parameters
s
Socket descriptor for a socket created by socket.
name
Pointer to the sockaddr structure with the endpoint address (IP address and a port) of the host to establish a connection to.
namelen
Length of the endpoint address in bytes.
Return Value
0 (zero) if the function succeeds, SOCKET_ERROR if the function fails
Use WSAGetLastError to retrieve a specific error code.
Remarks
For TCP sockets, connect attempts to establish a connection to the specified destination using a 3-way handshake. connect on a blocking socket returns when the connection is established of failed.
A non-blocking socket becomes ready to write when the connection has been established or has failed, which can be checked by the select function call with the socket descriptor in the write descriptors set.
There are significant differences in the way non-blocking connect is handled between the RT-TCP/IP Stack Microsoft Windows, as noted below.
- If the connection has not completed in a single connect call, the non-blocking connect sets an error code WSAEINPROGRESS in the RTSS application, not WSAEWOULDBLOCK.
- If the connection error occurs when an RTSS application uses the non-blocking connect, both read and write descriptor sets are notified and the exception set does not get notified. This is because the RT-TCP/IP Stack uses the exception descriptor set only for out-of-band TCP data notification.
- The recv call with MSG_PEEK flag can be used in conjunction with select to know whether the connection has completed or failed. If the connection has completed, the recv will succeed or fail with error code WSAEWOULDBLOCK. If the connection has failed, the error code, retrieved by WSAGetLastError, indicates the reason for the failed connection.
For a connectionless socket, no datagrams are exchanged. Instead, connect specifies a destination for subsequent sends on the socket, and restricts the socket to receiving datagrams from the specified remote address.
Setting Scope ID is necessary when connecting to a link-local IPv6 address, otherwise connect will fail.
connect is not supported for SOCK_RAW type sockets.
Requirements
Minimum Supported Version | RTX64 2013 |
Header | Winsock2.h |
Library | RtTcpip.lib |